// 所有的屏幕间距集合
var spanRange = ['P0.7', 'P0.95', 'P1.27', 'P1.46', 'P1.56', 'P1.59', 'P1.9', 'P2.38', 'P2.5', 'P2.54', 'P3', 'P4', 'P4.75', 'P5', 'P6', 'P7.625', 'P8', 'P10', 'P16'];
var currSet = {
    type: 2,        //工况：   1 安防监控 2 商用广告 3 门头广告 4 交通诱导 5 舞台租赁
    location: 1,     //室内外：   1 户外 2 室内 3 半户外
    install: 2,    //挂装方式： 1 落地式 2 墙挂式 3 单立柱 4 双立柱
    color: 3,          //色彩：     1 单色 2 双色 3 全彩
    span: 6,        //间距：    0.95, 1.27, 1.46, 1.56, 1.59, 1.9, 2.38, 2.5, 2.54, 3, 4, 4.75, 5, 6, 7.625, 8, 10, 16
    box: 3,   //组装方式：  1 原装箱体 2 组装-租赁箱体 3 组装-普通箱体 4 型材
    wide: 8,           //宽：
    high: 4.5,          //高：
    sync: 1,      //同步/异步：  1 同步 2 异步 3 门头异步
    toBottom: 0
};
var locationDict = {
    1: '户外', 2: '室内', 3: '半户外'
};
var options = {
    "2": {
        type: 2, location: 1, color: 3, span: 6, wide: 8, high: 4.5, install: 2, box: '3', sync: 1, toBottom: 0,
        forbid: [3]
    },
    "3": {
        type: 3, location: 3, color: 1, span: 10, wide: 3, high: 0.5, install: 2, box: 4, sync: 3, toBottom: 0,
        forbid: [2]
    },
    "5": {
        type: 5, location: 2, color: 3, span: 4, wide: 6, high: 4, install: 1, box: 2, sync: 1, toBottom: 0,
        forbid: [3]
    },
    "4": {
        type: 4, location: 1, color: 3, span: 10, wide: 3, high: 0.5, install: 2, box: 3, sync: 1, toBottom: 0,
        forbid: [2,3]
    },
    "1": {
        type: 1, location: 2, color: 3, span: 2.5, wide: 5, high: 3, install: 2, box: 1, sync: 1, toBottom: 0,
        forbid: [3]
    }
};

/** 根据使用场景配置默认项 */
function setInitOptions(type) {
    currSet = options[type];
    // 设置宽高
    $('.width-v').val( currSet.wide);
    $('.height-v').val(currSet.high);
    // 设置location
    $('.location-v').html(locationDict[currSet.location]);
    // 设置默认间距
    $('.span-v').val(currSet.span);
    $('.loc-option').find('p').show();
    for(var i=0;i<currSet.forbid.length;i++){
        var v = currSet.forbid[i];
        $('.loc-option').find('p[data-type="'+v+'"]').hide();
    }

}

var cityList = [{"letter":"A","list":["阿拉善盟","鞍山市","安庆市","安阳市","阿坝藏族羌族自治州","安顺市","阿里地区","安康市","阿克苏地区","阿勒泰地区","阿拉尔市","澳门特别行政区"]},{"letter":"B","list":["北京市","保定市","包头市","巴彦淖尔市","本溪市","白山市","白城市","蚌埠市","亳州市","滨州市","北海市","百色市","白沙黎族自治县","保亭黎族苗族自治县","巴中市","毕节市","保山市","宝鸡市","白银市","博尔塔拉蒙古自治州","巴音郭楞蒙古自治州","北屯市"]},{"letter":"C","list":["承德市","沧州市","长治市","赤峰市","长春市","常州市","滁州市","池州市","长沙市","常德市","郴州市","潮州市","崇左市","澄迈县","昌江黎族自治县","成都市","楚雄彝族自治州","昌都市","昌吉回族自治州"]},{"letter":"D","list":["大同市","大连市","丹东市","大庆市","大兴安岭地区","东营市","德州市","东莞市","儋州市","东方市","定安县","德阳市","达州市","大理白族自治州","德宏傣族景颇族自治州","迪庆藏族自治州","定西市"]},{"letter":"E","list":["鄂尔多斯市","鄂州市","恩施土家族苗族自治州"]},{"letter":"F","list":["抚顺市","阜新市","阜阳市","福州市","莆田市","抚州市","佛山市","防城港市"]},{"letter":"G","list":["赣州市","广州市","桂林市","贵港市","广元市","广安市","甘孜藏族自治州","贵阳市","甘南藏族自治州","果洛藏族自治州","固原市"]},{"letter":"H","list":["邯郸市","衡水市","呼和浩特市","呼伦贝尔市","葫芦岛市","哈尔滨市","鹤岗市","黑河市","淮安市","杭州市","湖州市","合肥市","淮南市","淮北市","黄山市","菏泽市","鹤壁市","黄石市","黄冈市","衡阳市","怀化市","惠州市","河源市","贺州市","河池市","海口市","红河哈尼族彝族自治州","汉中市","海东市","海北藏族自治州","黄南藏族自治州","海南藏族自治州","海西蒙古族藏族自治州","哈密市","和田地区"]},{"letter":"J","list":["晋城市","晋中市","锦州市","吉林市","鸡西市","佳木斯市","嘉兴市","金华市","景德镇市","九江市","吉安市","济南市","济宁市","焦作市","济源市","荆门市","荆州市","江门市","揭阳市","嘉峪关市","金昌市","酒泉市"]},{"letter":"K","list":["开封市","昆明市","克拉玛依市","克孜勒苏柯尔克孜自治州","喀什地区","可克达拉市","昆玉市"]},{"letter":"L","list":["廊坊市","临汾市","吕梁市","辽阳市","辽源市","连云港市","丽水市","六安市","龙岩市","莱芜市","临沂市","聊城市","洛阳市","娄底市","柳州市","来宾市","临高县","乐东黎族自治县","陵水黎族自治县","泸州市","乐山市","凉山彝族自治州","六盘水市","丽江市","临沧市","拉萨市","林芝市","兰州市","陇南市","临夏回族自治州"]},{"letter":"M","list":["牡丹江市","马鞍山市","茂名市","梅州市","绵阳市","眉山市"]},{"letter":"N","list":["南京市","南通市","宁波市","南平市","宁德市","南昌市","南阳市","南宁市","内江市","南充市","怒江傈僳族自治州","那曲地区"]},{"letter":"P","list":["盘锦市","萍乡市","平顶山市","濮阳市","攀枝花市","普洱市","平凉市"]},{"letter":"Q","list":["秦皇岛市","齐齐哈尔市","七台河市","衢州市","泉州市","青岛市","潜江市","清远市","钦州市","琼海市","琼中黎族苗族自治县","黔西南布依族苗族自治州","黔东南苗族侗族自治州","黔南布依族苗族自治州","曲靖市","庆阳市"]},{"letter":"R","list":["日照市","日喀则市"]},{"letter":"S","list":["石家庄市","朔州市","沈阳市","四平市","松原市","双鸭山市","绥化市","上海市","苏州市","宿迁市","绍兴市","宿州市","厦门市","三明市","上饶市","三门峡市","商丘市","十堰市","随州市","神农架林区","邵阳市","韶关市","深圳市","汕头市","汕尾市","三亚市","三沙市","遂宁市","山南市","商洛市","石嘴山市","石河子市","双河市"]},{"letter":"T","list":["天津市","唐山市","太原市","通辽市","铁岭市","通化市","泰州市","台州市","铜陵市","泰安市","漯河市","天门市","屯昌县","铜仁市","铜川市","天水市","吐鲁番市","塔城地区","图木舒克市","铁门关市","台湾省"]},{"letter":"W","list":["乌海市","乌兰察布市","无锡市","温州市","芜湖市","潍坊市","威海市","武汉市","梧州市","五指山市","文昌市","万宁市","文山壮族苗族自治州","渭南市","武威市","吴忠市","乌鲁木齐市","五家渠市"]},{"letter":"X","list":["邢台市","忻州市","兴安盟","锡林郭勒盟","徐州市","宣城市","新余市","新乡市","许昌市","信阳市","襄阳市","孝感市","咸宁市","仙桃市","湘潭市","湘西土家族苗族自治州","西双版纳傣族自治州","西安市","咸阳市","西宁市","香港特别行政区"]},{"letter":"Y","list":["阳泉市","运城市","营口市","延边朝鲜族自治州","伊春市","盐城市","扬州市","鹰潭市","宜春市","烟台市","宜昌市","岳阳市","益阳市","永州市","阳江市","云浮市","玉林市","宜宾市","雅安市","玉溪市","延安市","榆林市","玉树藏族自治州","银川市","伊犁哈萨克自治州"]},{"letter":"Z","list":["张家口市","朝阳市","镇江市","舟山市","漳州市","淄博市","枣庄市","郑州市","周口市","驻马店市","株洲市","张家界市","珠海市","湛江市","肇庆市","中山市","重庆市","自贡市","资阳市","遵义市","昭通市","张掖市","中卫市"]}];
var CityIndex = ["A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","W","X","Y","Z"];
var CityHots = ["北京市","长春市","常州市","长沙市","大连市","贵阳市","广州市","哈尔滨市","济南市","昆明市","宁波市","青岛市","上海市","深圳市","无锡市","西安市","厦门市","郑州市"];

$(document).ready(function () {
    // 渲染热门城市
    var hotDom = '';
    $.each(CityHots,function (index, value) {
        hotDom += '<div class="city-button" data-city="'+value+'">'+value+'</div>';
    });
    $('.line-1').html(hotDom);
    
    //渲染城市列表
    var cityDom = '';
    $.each(cityList,function (index,item) {
        var letter = '<div class="city-part" id="CitySelector-'+item.letter +'"><span class="city-list">'+item.letter +'</span>';
        var citys = '';
        $.each(item.list,function (i, city) {
            citys += '<div class="city-item" data-city="'+city+'">'+city+'</div>';
        });
        cityDom += letter + citys + '</div>';
    });
    $('.city-all').html(cityDom);
    
    // 渲染字母表
    var letDom = '';
    $.each(CityIndex,function (index, value) {
        letDom += '<a href="#CitySelector-'+value+'">'+value+'</a>';
    });
    $('.nav-bar').append(letDom);

});

function selected(e,obj){
    var selCity = e.target.getAttribute('data-city');
    if(selCity){
        globalData.selPos.city = selCity;
        globalData.selPos.prov = globalData.cityToProvince[selCity];
        $(".nav-bar").css("visibility","hidden");
        $('#CitySelector').css('left','100%');
        $(".city").html(selCity);
        $(".currCity").html(selCity);
    }
}
function getQuotation() {
    //显示计算板块
    var computedResult = $('#computed-Result');
    //请求报价接口
    var configs = dealOptions();
    computePrice(configs,{
        succ: function (json) {
            var div = 0,sys = 0;

            for(var i=0;i<json.prices.screen_products.length;i++){
                if(parseInt(json.prices.screen_products[i].product_type_id) == 1 || parseInt(json.prices.screen_products[i].product_type_id) == 6){
                    div = div + parseFloat(json.prices.screen_products[i].total);
                }else{
                    sys = sys + parseFloat(json.prices.screen_products[i].total);
                }
            }
            //模组1、箱体4、电源6
            div = div+ parseFloat(json.prices.assemble_products[0].total);

            var tt = formatInt(parseInt(json.total));
            div = formatInt(div);
            sys = formatInt(sys);
            var other = '议价';
            if(tt - div - sys>0){
                other = '&yen;'+ (tt - div - sys)
            }else{
                sys = tt -div;
            }
            // 根据价格区间乘以相应的系数
            if(tt>10000 && tt<=100000){
                // 随机区间（0.6-1）
                var randomNum = parseFloat((Math.random()*0.4+0.6).toFixed(1));
                tt = tt*randomNum;
                div = div*randomNum;
                sys = sys*randomNum;
                if(typeof other == "number"){
                    other = other*randomNum;
                }

            }else if(tt>100000 && tt<=300000){
                // 随机区间（0.4-0.7）
                var randomNum = parseFloat((Math.random()*0.3+0.4).toFixed(1));
                tt = tt*randomNum;
                div = div*randomNum;
                sys = sys*randomNum;
                if(typeof other == "number"){
                    other = other*randomNum;
                }
            }else if(tt>300000 && tt<=500000){
                // 随机区间（0.4-0.6）
                var randomNum = parseFloat((Math.random()*0.2+0.4).toFixed(1));
                tt = tt*randomNum;
                div = div*randomNum;
                sys = sys*randomNum;
                if(typeof other == "number"){
                    other = other*randomNum;
                }
            }else if(tt>500000){
                // 随机区间（0.2-0.6）
                var randomNum = parseFloat((Math.random()*0.4+0.2).toFixed(1));
                tt = tt*randomNum;
                div = div*randomNum;
                sys = sys*randomNum;
                if(typeof other == "number"){
                    other = other*randomNum;
                }
            }
            computedResult.find('.price-count span').html(tt);
            computedResult.find('.display-mod').html('&yen;'+div);
            computedResult.find('.control-mod').html('&yen;'+sys);
            computedResult.find('.other-mod').html(other);
            computedResult.show();
        },
        fail:function () {

        }
    })
}
/**预算取整*/
function formatInt(number) {
    var length = parseInt(number).toString().length;
    if (number < 100) {
        return Math.round(number);
    } else {
        return Math.round(number / Math.pow(10, length - 2)) * Math.pow(10, length - 2);
    }
}

/** 处理配置项，用来匹配后端需要的参数模型*/
function dealOptions() {
    var options = {};
    options.working = currSet.type;
    options.scene_type = currSet.location;
    options.install_way = currSet.install;
    options.color = currSet.color;
    options.spacing = currSet.span;
    options.assemble_way = currSet.box;
    options.wide = currSet.wide;
    options.high = currSet.high;
    options.transport = currSet.sync;
    options.ground_high = currSet.toBottom;

    return options
}


/** 百度统计事件
 * type: 1 ==  点击按钮就计算一次
 * type: 2 ==  手机号正确并点击按钮
 * type: 3 ==  手机号正确并提交草稿成功
 * */

function trackBaidu(type) {
    var src = 'http://m.maipingba.com/track/Click_Btn.html';

    if(type == 2){
        src = 'http://m.maipingba.com/track/Click_With_Mobile.html';
    }else if(type == 3){
        src = 'http://m.maipingba.com/track/Click_And_Submit.html';
    }else if(type == 4){
        src = 'http://m.maipingba.com/track/Click_MeiQia.html';
    }else if(type == 5){
        src = 'http://m.maipingba.com/track/Click_Call.html';
    }

    // 百度事件统计
    var iframe = '<div class="hide"><iframe src="'+src+'"></iframe>"</div>';
    $('body').append(iframe);

}

function checkPhone(phone) {
    if (!/^1(3|4|5|7|8)\d{9}$/.test(phone)) {
        return false;
    }
    return true;
}

// 点击电话按钮统计事件
function call() {
    trackBaidu(5);
}

// 提交需求草稿
function postDraft(data, cb) {
    ajax_post("/Client/Demand/postDraft", data, cb);
}
// 计算报价
function computePrice(data, cb) {
    ajax_post("/compute/prices", data, cb);
}

/** ajax请求方法 */
function ajax_post(url, data, cb) {
    var URL = "https://opc.wanpinghui.com/api" + url;
    $.ajax(URL, $.extend({
        method: 'POST',
        data: data,
        dataType: "json",
        timeout: 30000,
        xhrFields: {
            withCredentials: true
        },
        headers: {
            'Accept': 'application/x-www-form-urlencoded',
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        contentType: 'application/x-www-form-urlencoded',
        success: function success(json, status, xhr) {
            cb && cb.always && cb.always(json);
            if (json.status == "200") {
                cb && cb.succ && cb.succ(json);
            } else {
                cb && cb.fail && cb.fail(json);
            }
        },
        error: function error(xhr, status, thrown) {
            console.log("[!err!](" + url + "): status: " + xhr.status + ", msg: " + thrown);
            cb && cb.fail && cb.fail();
            console.error("接口[" + url + "]：失败");
        }
    }));
}